package com.itye.deom1.view;

import com.itye.deom1.ClassRoom;
import com.itye.deom1.Group;
import com.itye.deom1.Student;
import com.itye.deom1.RandomPickerFrame;
import com.itye.deom1.until.Constant;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class MainFrame extends JFrame {
    private JTabbedPane tabbedPane = new JTabbedPane();
    private RandomPickerFrame randomPickerFrame;

    public MainFrame() {
        this.setTitle("班级管理系统");
        this.setSize(800, 600);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 添加各个功能面板
        tabbedPane.addTab("选择班级", new ChangeClassPanel(this));
        tabbedPane.addTab("新增班级", new ClassAddPanel());
        tabbedPane.addTab("班级列表", new ClassListPanel());
        tabbedPane.addTab("新增小组", new GroupAddPanel());
        tabbedPane.addTab("小组列表", new GroupListPanel());
        tabbedPane.addTab("随机点名", new RandomStudentPanel());  // 添加随机点名面板
        
        // 创建随机抽取按钮面板
        JPanel randomPanel = new JPanel();
        JButton randomButton = new JButton("打开随机抽取");
        randomButton.addActionListener(e -> {
            if (randomPickerFrame == null) {
                // 创建测试数据的ClassRoom数组
                ClassRoom classRoom1 = new ClassRoom("七班");
                ClassRoom classRoom2 = new ClassRoom("八班");
                
                // 从Constant中获取已有的学生和小组数据
                for (Group group : Constant.groups.keySet()) {
                    List<Student> students = Constant.groups.get(group);
                    // 先添加学生到Group中
                    for (Student student : students) {
                        group.addStudent(student);
                    }
                    // 再添加Group到ClassRoom中
                    classRoom1.addGroup(group);
                    classRoom2.addGroup(group);
                }
                
                for (Student student : Constant.students) {
                    classRoom1.addStudent(student);
                    classRoom2.addStudent(student);
                }
                
                ClassRoom[] classRooms = {classRoom1, classRoom2};
                randomPickerFrame = new RandomPickerFrame(classRooms);
            }
            randomPickerFrame.setVisible(true);
        });
        randomPanel.add(randomButton);
        tabbedPane.addTab("随机抽取", randomPanel);

        this.add(tabbedPane);
        this.setVisible(true);
    }

    public void setTitle(String title) {
        super.setTitle(title);
    }
}